<?php
declare (strict_types = 1);

namespace app\demo\controller;

use app\demo\model\Login;
use app\demo\model\Type;
use app\demo\validate\User;
use think\cache\driver\Redis;
use think\facade\Session;
use think\facade\View;
use think\Request;
// 引入鉴权类
use Qiniu\Auth;
// 引入上传类
use Qiniu\Storage\UploadManager;

class Text
{
    public function login(){
        return \view();
    }

    public function dologin(){
        $account = \request()->post('account');
        $password = \request()->post('password');
        $res = Login::where('account',$account)->find();
        if (!$res){
            return json(['code'=>2001,'msg'=>'账号不存在']);
        }elseif ($password != $res['password']){
            return  json(['code'=>2002,'msg'=>'密码错误']);
        }else{
            Session::set('name',['account'=>$account]);
            return redirect('text');
        }
    }


    public function text(){
        if (!Session::get('name')){
            return redirect('login');
        }
        $data = Type::select();
        View::assign('data',$data);
        return view();
    }

    public function add(){
        try {
            if (!Session::get('name')){
                return redirect('login');
            }
            $data = \request()->all();
            validate(User::class)->check($data);
            $data['logo'] = \think\facade\Filesystem::disk('public')->putFile( 'img', $data['logo']);
            $image = \think\Image::open('./storage/'.$data['logo']);
            $pace = time().'.'.'png';
            $image->thumb(150, 150)->save('./storage/'.$pace);
            $img = \think\Image::open('./storage/'.$pace);
            $pace_a = time().'.'.'png';
            $ttf = getcwd().'./'.'1.ttf';
            $img->text('week2',$ttf,20,'#ffffff')->save('./storage/'.$pace_a);
            $ser = \app\demo\model\Text::create([
                'title' => $data['title'],
                'text' => $data['text'],
                'type' => $data['type'],
                'logo' => $pace_a,
                'tuijian' => $data['tuijian'],
                'order' => $data['order'],
                'state' =>$data['state']
            ]);
            if ($ser){
                return redirect('show');
            }
        }catch (\Exception $exception){
            return json(['code'=>2001,'msg'=>$exception->getMessage()]);
        }

    }


    public function show(){
        $title = \request()->get('title');
        $type = \request()->get('type');
        $state = \request()->get('state');
        $where = [];
        if (!empty($title)){
            $where[] = ['title','like',"%$title%"];
        }
        if (!empty($type)){
            $where[] = ['type','=',"%$type%"];
        }
        if (!empty($state)){
            $where[] = ['state','=',"%$state%"];
        }
        $data = \app\demo\model\Text::order('order','desc')->where($where)->with('type')->paginate(2);
        View::assign('data',$data);
        return \view();
    }

    public function del(){
        $id = \request()->get('id');
        $data = \app\demo\model\Text::where('id',$id)->find();
        if ($data['state'] == 1){
            return json(['code'=>2002,'msg'=>'显示状态不能删除']);
        }
        $res = $data->delete($data);
        if ($res){
            return redirect('show');
        }
    }

    public function delete(){
        $data = \app\demo\model\Text::onlyTrashed()->select();
        View::assign('data',$data);
        return \view();
    }

    public function restore(){
        $id = \request()->get('id');
        $res = \app\demo\model\Text::onlyTrashed()->find($id);
       $del =  $res->restore();
        if ($del){
            return redirect('show');
        }
    }
}
